package com.example.sunming.warning;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import org.apache.commons.lang3.StringUtils;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @description: 解析mdrs文本
 * @date: 2020/12/7
 * @author: sunming
 */
public class ParseMdrsText {

    public static void main(String[] args) {
        List<ParseMdrsTextBO> parse = parse("D:/PWSMR9E07.181.doc");
        System.out.println(parse);

    }

    /**
     * 解析mdrs文件
     *
     * @param filePath
     */
    public static List<ParseMdrsTextBO> parse(String filePath) {
        // 读取rtf文件
        String rtf = readText(filePath);
        // 获取表格上边的文本内容
        String rtfHeaderText = ReadFromRtfUtil.readTitleFromRtf(rtf);
        // 封装地区和时间bo
        AreaAndTimeBO areaAndTimeBO = toAreaAndTimeBO(rtfHeaderText);
        // 获取表格内容
        List<String> textFromRtf = ReadFromRtfUtil.getTextFromRtf(rtf);
        // 封装解析后的实体类
        List<ParseMdrsTextBO> textBOS = convertToBO(textFromRtf, areaAndTimeBO);
        return textBOS;
    }


    /**
     * 解析文本中地区和制作时间实体类
     *
     * @param rtfHeaderText
     * @return
     */
    public static AreaAndTimeBO toAreaAndTimeBO(String rtfHeaderText) {
        AreaAndTimeBO areaAndTimeBO = new AreaAndTimeBO();
        String[] split = rtfHeaderText.split("\n");
        for (String s : split) {
            if (s.contains("地区")) {
                String[] s1 = s.split(" ");
                List<String> collect = Arrays.stream(s1).filter(a -> {
                    return !a.equals("");
                }).collect(Collectors.toList());
                String area = collect.get(0);
                areaAndTimeBO.setArea(area.substring(3));
                String time = collect.get(1);
                areaAndTimeBO.setReleaseTime(time.substring(5));
            }
        }
        return areaAndTimeBO;
    }

    /**
     * 封装解析mdrs文本后的对象
     *
     * @param texts
     * @return
     */
    public static List<ParseMdrsTextBO> convertToBO(List<String> texts, AreaAndTimeBO areaAndTimeBO) {
        List<ParseMdrsTextBO> textBOS = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(texts)) {
            for (int i = 1; i < texts.size(); i++) {
                String text = texts.get(i);
                String[] split = text.split("\n");
                ParseMdrsTextBO parseMdrsTextBO = new ParseMdrsTextBO();
                parseMdrsTextBO.setArea(areaAndTimeBO.getArea());
                // 获取时间格式的发布时间，预报开始和结束时间
                String existTime = split[2].equals("NA") ? "" : split[2].trim();
                AreaAndTimeBO.StartAndEndTimeBO startAndEndTimeBO = ParseMdrsTimeUtil.obtainTime(areaAndTimeBO.getReleaseTime(), existTime);
                parseMdrsTextBO.setReleaseTime(startAndEndTimeBO.getReleaseTime());
                parseMdrsTextBO.setStartTime(startAndEndTimeBO.getStartTime());
                parseMdrsTextBO.setEndTime(startAndEndTimeBO.getEndTime());
                parseMdrsTextBO.setAirportCode(split[0].equals("NA") ? "" : split[0].trim());
                parseMdrsTextBO.setWeather(split[1].equals("NA") ? "" : split[1].trim());
                parseMdrsTextBO.setRange(split[3].equals("NA") ? "" : split[3].trim());
                parseMdrsTextBO.setHappen(split[4].equals("NA") ? "" : split[4].trim());
                parseMdrsTextBO.setRemark(split[5].equals("NA") ? "" : split[5].trim());
                textBOS.add(parseMdrsTextBO);
            }
        }
        // 如果机场编码没有，则用前一个的
        for (int i = 0; i < textBOS.size(); i++) {
            ParseMdrsTextBO bo = textBOS.get(i);
            if (StringUtils.isBlank(bo.getAirportCode())) {
                bo.setAirportCode(textBOS.get(i - 1).getAirportCode());
            }
        }
        return textBOS;
    }


    /**
     * 读取rtf文件
     *
     * @param filePath 文件路径
     * @return 读取文件的内容
     */
    public static String readText(String filePath) {
        StringBuilder builder = new StringBuilder();
        try {
            File file = new File(filePath);
            FileInputStream inputStream = new FileInputStream(file);
            InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8");
            BufferedReader br = new BufferedReader(reader);
            String line = null;
            while ((line = br.readLine()) != null) {
                builder.append(line);
            }
            reader.close();
            br.close();
            inputStream.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return builder.toString();

    }

}
